﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Windows.Forms;
using RPGOnline.Controls.Chat;
using RPGOnline.Controls.Chat.Events;
using RPGOnline.Controls.Map;
using RPGOnline.Controls.Map.Windows;
using RPGOnline.Filters;
using RPGOnline.Remoting;
using RPGOnline.Remoting.Entities;
using RPGOnline.Remoting.Events;
using WeifenLuo.WinFormsUI.Docking;

namespace RPGOnline
{
    public partial class MainForm : Form
    {
        private UserManager m_userManager;
        private ChatManager m_chatManager;
        private string m_serverIP = "Balrog994-BOOK";
        private int m_serverPort = 12345;
        private SessionContext m_sessionContext;

        public MainForm()
        {
            InitializeComponent();
        }

        public MainForm(string serverIp, int serverPort, string userName, bool isMaster)
            : this()
        {
            m_serverIP = serverIp;
            m_serverPort = serverPort;
            m_userManager = new UserManager(userName, new Font("Microsoft Sans Serif", 8), Color.Magenta);
            m_sessionContext = new SessionContext();
            m_userManager.IsMaster = isMaster;

            m_chatManager = new ChatManager(m_serverIP, m_serverPort);
            ChatControl chatControl = new ChatControl();
            new ChatControlPresenter(
                m_sessionContext,
                m_userManager, 
                m_chatManager,
                chatControl
            );
            chatControl.Show(this);

            MapControl mapControl = new MapControl();
            TilesetPaintingForm tilesetPaintingForm = new TilesetPaintingForm();
            MapPropertiesForm mapPropertiesForm = new MapPropertiesForm();
            new MapControlPresenter(
                mapControl, 
                new TilesetPainter(
                    mapControl, 
                    tilesetPaintingForm
                ),
                mapPropertiesForm
            );

            mapControl.Show(dockPanel2, DockState.Document);
            mapControl.DockAreas = DockAreas.Document;

            mapPropertiesForm.Show(dockPanel2, DockState.DockRight);
            mapPropertiesForm.DockAreas = DockAreas.DockBottom | DockAreas.DockLeft | DockAreas.DockRight |
                                          DockAreas.DockBottom;

            tilesetPaintingForm.Show(dockPanel2, DockState.DockRight);
            tilesetPaintingForm.DockAreas = DockAreas.DockBottom | DockAreas.DockLeft | DockAreas.DockRight |
                                            DockAreas.DockBottom;

            chatControl.Show(dockPanel2, DockState.DockBottom);
            chatControl.DockAreas = DockAreas.DockBottom | DockAreas.DockLeft | DockAreas.DockRight |
                                            DockAreas.DockBottom;
            m_chatManager.OnRegistrationFailed += OnRegistrationFailed;
            m_chatManager.Notify.RegisterUser(m_userManager.CurrentUser, isMaster);
            m_chatManager.Notify.RequestSessionUpdate();
        }

        private void OnRegistrationFailed(object sender, EventArgs e)
        {
            MessageBox.Show(this, "Could not register to the RPGOnline network!", "Registration Failed",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(m_chatManager != null)
                m_chatManager.Notify.UnregisterUser(m_userManager.CurrentUser);
        }
    }
}
